import json
import random
import requests
from bs4 import BeautifulSoup

# 请求头部
headers = [
    {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'},
    {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Firefox/91.0'},
    {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'},
    {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'}
]
# ip地址
ips = [
    '58.192.0.1','58.193.0.2','58.194.0.3','58.195.0.4','61.132.0.1','61.133.0.2','61.134.0.3','61.135.0.4',
    '114.217.0.1','114.218.0.2','114.219.0.3','114.220.0.4','221.226.0.1','221.227.0.2','221.228.0.3','221.229.0.4'
]

# 获取随机ip
def ip_random():
    proxy = random.choice(ips)
    proxys = {
        "http": "http://" + proxy
    }
    return proxys

# 传入某部电影url，获取电影主页数据
def get_url_movie(url):
    movie_page = requests.get(url, headers=random.choice(headers), proxies=ip_random())
    # 提取内容
    soup = BeautifulSoup(movie_page.text, 'lxml')

    # 电影名称
    movie = soup.find('div', id='content')
    movie_data['名称'] = movie.find('h1').find('span').text

    # 电影信息
    movie_message = soup.find('div', id='info').text.strip()
    for line in movie_message.split('\n'):
        key, value = line.split(": ", 1)
        movie_data[key] = value

    # 豆瓣评分
    movie_rate = movie.find('div', id='interest_sectl')
    movie_data['评分'] = movie_rate.find('strong', class_='ll rating_num').text
    rating_people = movie_rate.find('a', class_='rating_people')
    # 如果没有评分则找不到评价人数，设为空
    if rating_people:
        movie_data['评价人数'] = rating_people.find('span').text
    else:
        movie_data['评价人数'] = ''

    # 文件写入
    print(movie_data)
    json_movie_data = json.dumps(movie_data, ensure_ascii=False)
    with open('a.txt', 'a', encoding='utf-8') as file:
        file.write(json_movie_data)
        file.write('\n')


url = 'https://movie.douban.com/j/search_subjects'
params = {
    'type': 'movie',            # 指定搜索类型  (movie电影，tv电视剧)
    'tag': '热门',               # 搜索的标签
    'sort': 'recommend',        # 排序方式，这里是“recommend”，意味着按照推荐度排序
    'page_limit': '500',        # 每页显示的结果数量
    'page_start': '0'           # 起始个数，这里从第0个开始
}

# 发送请求
response = requests.get(url, params=params, headers=random.choice(headers))

# 检查响应状态码：如果请求成功遍历所有电影解析电影数据
if response.status_code == 200:
    # 保存提取到的电影数据
    movie_data = {}
    datas = response.json()
    cnt = 1
    print("共获取到{}条数据".format(len(datas['subjects'])))
    for data in datas['subjects']:
        print("第{}条数据: {}".format(cnt, data))
        cnt += 1
        get_url_movie(data['url'])
else:
    print(f'请求失败，状态码：{response.status_code}')

